﻿using DevComponents.DotNetBar;
using Kimd.Common;
using Kimd.Machine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace Kimd.Motion
{
    public partial class FormTray : Office2007Form
    {
        private string _moduleName;
        private string _xName;
        private string _yName;
        private string _zName;
        private string _rName;

        public Tray EditTray;
        public bool IsLoad { get; set; }
        public bool IsMultiNozzle { get; set; }
        public bool CornerChanged { get; set; }
        public FormTray(string xyNames, string zName, string rName, string moduleName, Tray tray)
        {
            InitializeComponent();
            this._xName = xyNames.Split(',')[0];
            this._yName = xyNames.Split(',')[1];
            this._zName = zName;
            this._rName = rName;
            this._moduleName = moduleName;
            this.EditTray = tray;
            this.EnableGlass = false;
        }

        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            if (!IsLoad)
            {
                if (string.IsNullOrEmpty(tB_TrayName.Text))
                {
                    SystemInfo.ShowToastInfo(this, "名称不能为空", true);
                    return;
                }
                if (MachineResource.Instance.ModuleTrays != null &&
                   MachineResource.Instance.ModuleTrays.ContainsKey(_moduleName) &&
                   MachineResource.Instance.ModuleTrays[_moduleName].ContainsKey(tB_TrayName.Text))
                {
                    SystemInfo.ShowToastInfo(this, "已存在同名矩阵点", true);
                    return;
                }
                if (cB_Direction.SelectedIndex == -1)
                {
                    MessageBox.Show("未选择矩阵排列方向", "矩阵点位", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            if(EditTray == null)
                EditTray = new Tray();
            EditTray.XAxisName = _xName;
            EditTray.YAxisName = _yName;
            EditTray.ZAxisName = _zName;
            EditTray.RAxisName = _rName;
            EditTray.RowCount = Convert.ToInt32(nUD_RowCount.Value);
            EditTray.ColCount = Convert.ToInt32(nUD_ColCount.Value);
            EditTray.Name = tB_TrayName.Text;
            EditTray.NozzleCount = (int)nUD_NozzleCount.Value;
            EditTray.OffsetX = (double)nUD_NozzleOffsetX.Value;
            EditTray.OffsetY = (double)nUD_NozzleOffsetY.Value;
            EditTray.NozzleZ = (double)nUD_Z.Value;
            EditTray.NozzleR = (double)nUD_R.Value;
            CornerChanged = isChanged;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void FormTray_Load(object sender, EventArgs e)
        {
            isLoading = true;
            cB_Direction.Items.Clear();
            foreach (Enum @enum in Enum.GetValues(typeof(TrayDir)))
            {
                cB_Direction.Items.Add(EnumHelper.GetEnumDescriptionFromValue(@enum));
            }
            cB_XAxisName.Items.Clear();
            cB_XAxisName.Items.AddRange(MachineResource.Instance.MachineAxis.Keys.ToArray());
            cB_YAxisName.Items.Clear();
            cB_YAxisName.Items.AddRange(MachineResource.Instance.MachineAxis.Keys.ToArray());
            cB_ZAxisName.Items.Clear();
            cB_ZAxisName.Items.AddRange(MachineResource.Instance.MachineAxis.Keys.ToArray());
            cB_RAxisName.Items.Clear();
            cB_RAxisName.Items.AddRange(MachineResource.Instance.MachineAxis.Keys.ToArray());

            cB_XAxisName.SelectedIndex = cB_XAxisName.Items.IndexOf(_xName);
            cB_YAxisName.SelectedIndex = cB_YAxisName.Items.IndexOf(_yName);
            cB_ZAxisName.SelectedIndex = cB_XAxisName.Items.IndexOf(_zName);
            cB_RAxisName.SelectedIndex = cB_XAxisName.Items.IndexOf(_rName);

            if (EditTray != null)
            {
                tB_TrayName.Text = EditTray.Name;
                nUD_RowCount.Value = EditTray.RowCount;
                nUD_ColCount.Value = EditTray.ColCount;
                nUD_NozzleCount.Value = EditTray.NozzleCount;
                nUD_NozzleOffsetX.Value = Convert.ToDecimal(EditTray.OffsetX);
                nUD_NozzleOffsetY.Value = Convert.ToDecimal(EditTray.OffsetY);
                nUD_Z.Value = Convert.ToDecimal(EditTray.NozzleZ);
                nUD_R.Value = Convert.ToDecimal(EditTray.NozzleR);
                cB_Direction.SelectedIndex = cB_Direction.Items.IndexOf(EnumHelper.GetEnumDescriptionFromValue(EditTray.Direction));

                nUD_LeftUpX.Value = Convert.ToDecimal(EditTray.LeftUpPoint.X);
                nUD_LeftUpY.Value = Convert.ToDecimal(EditTray.LeftUpPoint.Y);
                nUD_RightUpX.Value = Convert.ToDecimal(EditTray.RightUpPoint.X);
                nUD_RightUpY.Value = Convert.ToDecimal(EditTray.RightUpPoint.Y);
                nUD_LeftDownX.Value = Convert.ToDecimal(EditTray.LeftDownPoint.X);
                nUD_LeftDownY.Value = Convert.ToDecimal(EditTray.LeftDownPoint.Y);
                nUD_RightDownX.Value = Convert.ToDecimal(EditTray.RightDownPoint.X);
                nUD_RightDownY.Value = Convert.ToDecimal(EditTray.RightDownPoint.Y);

                cB_XAxisName.SelectedIndex = cB_XAxisName.Items.IndexOf(_xName);
                cB_YAxisName.SelectedIndex = cB_YAxisName.Items.IndexOf(_yName);
                cB_ZAxisName.SelectedIndex = cB_XAxisName.Items.IndexOf(_zName);
                cB_RAxisName.SelectedIndex = cB_XAxisName.Items.IndexOf(_rName);
            }
            isLoading = false;
        }

        private void cB_Direction_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cB_Direction.SelectedIndex == -1)
                return;
            if (EditTray == null)
            {
                EditTray = new Tray();
                EditTray.XAxisName = _xName;
                EditTray.YAxisName = _yName;
                EditTray.ZAxisName = _zName;
                EditTray.RAxisName = _rName;
            }
            EditTray.Direction = EnumHelper.GetEnumValueFromDescription<TrayDir>(cB_Direction.Text);
        }

        bool isChanged = false;
        bool isLoading = false;
        private void PositionValueChanged(object sender, EventArgs e)
        {
            if (isLoading)
                return;
            if (EditTray == null)
                return;
            isChanged = true;
            if ((sender as NumericUpDown).Name.Equals(nUD_LeftUpX.Name))
            {
                EditTray.LeftUpPoint = new PointF(Convert.ToSingle(nUD_LeftUpX.Value), Convert.ToSingle(nUD_LeftUpY.Value));
            }
            else if ((sender as NumericUpDown).Name.Equals(nUD_LeftUpY.Name))
            {
                EditTray.LeftUpPoint = new PointF(Convert.ToSingle(nUD_LeftUpX.Value), Convert.ToSingle(nUD_LeftUpY.Value));
            }
            else if ((sender as NumericUpDown).Name.Equals(nUD_RightUpX.Name))
            {
                EditTray.RightUpPoint = new PointF(Convert.ToSingle(nUD_RightUpX.Value), Convert.ToSingle(nUD_RightUpY.Value));
            }
            else if ((sender as NumericUpDown).Name.Equals(nUD_RightUpY.Name))
            {
                EditTray.RightUpPoint = new PointF(Convert.ToSingle(nUD_RightUpX.Value), Convert.ToSingle(nUD_RightUpY.Value));
            }
            else if ((sender as NumericUpDown).Name.Equals(nUD_LeftDownX.Name))
            {
                EditTray.LeftDownPoint = new PointF(Convert.ToSingle(nUD_LeftDownX.Value), Convert.ToSingle(nUD_LeftDownY.Value));
            }
            else if ((sender as NumericUpDown).Name.Equals(nUD_LeftDownY.Name))
            {
                EditTray.LeftDownPoint = new PointF(Convert.ToSingle(nUD_LeftDownX.Value), Convert.ToSingle(nUD_LeftDownY.Value));
            }
            else if ((sender as NumericUpDown).Name.Equals(nUD_RightDownX.Name))
            {
                EditTray.RightDownPoint = new PointF(Convert.ToSingle(nUD_RightDownX.Value), Convert.ToSingle(nUD_RightDownY.Value));
            }
            else if ((sender as NumericUpDown).Name.Equals(nUD_RightDownY.Name))
            {
                EditTray.RightDownPoint = new PointF(Convert.ToSingle(nUD_RightDownX.Value), Convert.ToSingle(nUD_RightDownY.Value));
            }
        }

        private void cB_XAxisName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if((sender as ComboBox).Name.Equals("cB_XAxisName"))
            {
                _xName = (sender as ComboBox).SelectedItem as string;
            }
            else if ((sender as ComboBox).Name.Equals("cB_YAxisName"))
            {
                _yName = (sender as ComboBox).SelectedItem as string;
            }
            else if ((sender as ComboBox).Name.Equals("cB_ZAxisName"))
            {
                _zName = (sender as ComboBox).SelectedItem as string;
            }
            else if ((sender as ComboBox).Name.Equals("cB_RAxisName"))
            {
                _rName = (sender as ComboBox).SelectedItem as string;
            }
        }

        private void btn_Get1_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("获取将改变当前点位位置，确定继续吗?", "获取", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            { 
                string name = (sender as Button).Name;
                switch (name)
                {
                    case "btn_Get1":
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.XAxisName))
                            return;
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.YAxisName))
                            return;
                        nUD_LeftUpX.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.XAxisName].Position);
                        nUD_LeftUpY.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.YAxisName].Position);
                        break;
                    case "btn_Get2":
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.XAxisName))
                            return;
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.YAxisName))
                            return;
                        nUD_RightUpX.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.XAxisName].Position);
                        nUD_RightUpY.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.YAxisName].Position);
                        break;
                    case "btn_Get3":
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.XAxisName))
                            return;
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.YAxisName))
                            return;
                        nUD_LeftDownX.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.XAxisName].Position);
                        nUD_LeftDownY.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.YAxisName].Position);
                        break;
                    case "btn_Get4":
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.XAxisName))
                            return;
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.YAxisName))
                            return;
                        nUD_RightDownX.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.XAxisName].Position);
                        nUD_RightDownY.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.YAxisName].Position);
                        break;
                    case "btn_Get5":
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.ZAxisName))
                            return;
                        nUD_Z.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.ZAxisName].Position);
                        break;
                    case "btn_Get6":
                        if (EditTray == null || string.IsNullOrEmpty(EditTray.RAxisName))
                            return;
                        nUD_R.Value = Convert.ToDecimal(MachineResource.Instance.MachineAxis[EditTray.RAxisName].Position);
                        break;
                }
            }
        }
    }
}
